﻿
#include"tcs.h"
void SetPos(short x, short y)
{
	COORD pos = { x,y };
	HANDLE houtput = NULL;
	houtput = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(houtput, pos);
}
void WelcomeToGame()
{
	SetPos(40, 15);
	printf("欢迎来到贪吃蛇\n");
	SetPos(40, 25);
	system("pause");
	system("cls");
	SetPos(25, 12);
	printf("⽤ ↑ . ↓ . ← . → 分别控制蛇的移动， F3为加速，F4为减速\n");
	SetPos(25, 13);
	printf("加速获得的分数更高\n");
	SetPos(40, 25);
	system("pause");
	system("cls");
}
void CreatMap()
{
	SetPos(0, 0);
	for (int i = 0; i < 58; i = i + 2)
	{
		wprintf(L"%c", WALL);
	}
	SetPos(0, 26);
	for (int i = 0; i < 58; i= i + 2)
	{
		wprintf(L"%c", WALL);
	}
	for (int i = 1; i < 26; i++)
	{
		SetPos(0, i);
		wprintf(L"%c", WALL);
	}
	for (int i = 1; i < 26; i++)
	{
		SetPos(56, i);
		wprintf(L"%c", WALL);
	}
}
void InitSnake(pSnake ps)
{
	pSnakeNode cur = NULL;
	for (int i = 0; i < 5; i++)
	{
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("snakenode don't get:");
			return;
		}
		cur->next = NULL;
		cur->x = POS_X + i * 2;
		cur->y = POS_Y;
		if (ps->_pbody == NULL)
		{
			ps->_pbody = cur;
		}
		else
		{
			cur->next = ps->_pbody;
			ps->_pbody = cur;
		}
	}

	//print snake
	cur = ps->_pbody;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", BODY);
		cur = cur->next;
	}
	//init snake data
	ps->_dir = RIGHT;
	ps->_foodweight = 100;
	ps->_score = 0;
	ps->_sleeptime = 200;
	ps->_status = OK;
}
void CreatFood(pSnake ps)
{
	int x = 0;
	int y = 0;
	again:
	do
	{
		x = rand() % 53+2;
		y = rand() % 25+1;
	} while (x % 2 != 0);
	pSnakeNode cur = ps->_pbody;
	while (cur)
	{
		if (cur->x == x && cur->y == y)
		{
			goto again;
		}
		cur = cur->next;
	}
	pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pFood == NULL)
	{
		perror("creat pfood failed,fool!\n");
		return;
	}
	else
	{
		pFood->x = x;
		pFood->y = y;
		SetPos(pFood->x, pFood->y);
		wprintf(L"%c", FOOD);
		ps->_pfood = pFood;
	}
}
void PrintHelpInfo()
{
	SetPos(64, 15);
	printf("不能创墙，也不能创自己");
	SetPos(64, 16);
	printf("用↑.↓.←.→分别控制蛇的移动.");
	SetPos(64, 17);
	printf("F3 为加速，F4 为减速\n");
	SetPos(64, 18);
	printf("ESC ：退出游戏.space：暂停游戏.");
	SetPos(64, 20);
	printf("江子龙@版权");
}
void pause()
{
	while (1)
	{
		Sleep(300);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}
}
int NextIsFood(pSnakeNode psn, pSnake ps)
{
	return(psn->x == ps->_pfood->x) && (psn->y == ps->_pfood->y);
}
void EatFood(pSnakeNode psn, pSnake ps)
{
	psn->next = ps->_pbody;
	ps->_pbody = psn;
	pSnakeNode cur = ps->_pbody;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", BODY);
		cur = cur->next;
	}
	ps->_score += ps->_foodweight;
	free(ps->_pfood);
	CreatFood(ps);
}
void NotFood(pSnakeNode psn, pSnake ps)
{
	psn->next = ps->_pbody;
	ps->_pbody = psn;
	pSnakeNode cur = ps->_pbody;
	while (cur->next->next)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", BODY);
		cur = cur->next;
	}
	SetPos(cur->next->x, cur->next->y);
	printf("  ");
	free(cur->next);
	cur->next = NULL;
}
int KillByWall(pSnake ps)
{
	if ((ps->_pbody->x == 0) || (ps->_pbody->x == 56) || (ps->_pbody->y == 0) || (ps->_pbody->y == 26))
	{
		ps->_status = KILL_BY_WALL;
		return 1;
	}
	return 0;
}
int KillBySelf(pSnake ps)
{
	pSnakeNode cur = ps->_pbody->next;
	while (cur)
	{
		if ((ps->_pbody->x == cur->x) && (ps->_pbody->y == cur->y))
		{
			ps->_status = KILL_BY_SELF;
			return 1;
		}
		cur = cur->next;
	}
	return 0;
}
void SnakeMove(pSnake ps)
{
	pSnakeNode pNextNode = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pNextNode == NULL)
	{
		perror("snake move open falied");
		return;
	}

	switch (ps->_dir)
	{
	case UP:
	{
		pNextNode->y = ps->_pbody->y - 1;
		pNextNode->x = ps->_pbody->x;
	}
		break;
	case DOWN:
	{
		pNextNode->y = ps->_pbody->y + 1;
		pNextNode->x = ps->_pbody->x;
	}
		break;
	case LEFT:
	{
		pNextNode->y = ps->_pbody->y ;
		pNextNode->x = ps->_pbody->x - 2;
	}
		break;
	case RIGHT:
	{
		pNextNode->y = ps->_pbody->y;
		pNextNode->x = ps->_pbody->x + 2;
	}
		break;
	default:
		break;
	}
	if (NextIsFood(pNextNode, ps))
	{
		EatFood(pNextNode, ps);
	}
	else
	{
		NotFood(pNextNode, ps);
	}
	KillBySelf(ps);
	KillByWall(ps);
}
void GameStart(pSnake ps)
{
	system("mode con cols=100 lines=30");
	system("title 贪吃蛇");
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(hOutput, &CursorInfo);
	CursorInfo.bVisible = false;
	SetConsoleCursorInfo(hOutput, &CursorInfo);
	WelcomeToGame();
	CreatMap();
	InitSnake(ps);
	CreatFood(ps);
}
void GameRun(pSnake ps)
{
	PrintHelpInfo();
	do
	{
		SetPos(64, 10);
		printf("得分：%d ", ps->_score);
		printf("每个食物得分：%d", ps->_foodweight);
		if (KEY_PRESS(VK_UP) && ps->_dir != DOWN)
		{
			ps->_dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->_dir != UP)
		{
			ps->_dir = DOWN;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_dir != LEFT)
		{
			ps->_dir = RIGHT;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_dir != RIGHT)
		{
			ps->_dir = LEFT;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			pause();
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			ps->_status = END_NORMAL;
			break;
		}
		else if (KEY_PRESS(VK_F3))
		{
			if (ps->_sleeptime >= 50)
			{
				ps->_sleeptime -= 30;
				ps->_foodweight += 20;
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			if (ps->_sleeptime < 350)
			{
				ps->_sleeptime += 30;
				ps->_foodweight -= 20;
				if (ps->_sleeptime == 350)
				{
					ps->_foodweight = 1;
				}
			}
			
		}
		Sleep(ps->_sleeptime);
		SnakeMove(ps);

	} while (ps->_status == OK);
}
void GameEnd(pSnake ps)
{
	pSnakeNode cur = ps->_pbody;
	SetPos(24, 12);
	switch (ps->_status)
	{
	case KILL_BY_SELF:
		printf("你创了自己！,游戏结束\n");
		break;
	case KILL_BY_WALL:
		printf("你创了墙！game over\n");
		break;
	case END_NORMAL:
		printf("游戏正常结束!\n");
		break;
	}
	while (cur)
	{
		pSnakeNode del = cur;
		cur = cur->next;
		free(del);
	}
}